package com.example.fuluchou.service.activitystatus.operator;

import com.example.fuluchou.dao.dataobject.ActivityDO;
import com.example.fuluchou.dao.mapper.ActivityMapper;
import com.example.fuluchou.dao.mapper.ActivityPrizeMapper;
import com.example.fuluchou.service.dto.ConvertActivityStatusDTO;
import com.example.fuluchou.service.enums.ActivityPrizeStatusEnum;
import com.example.fuluchou.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 活动模块
 */
@Component
public class ActivityOperator extends AbstractActivityOperator {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActicityId();
        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(null == activityId
                || null == targetStatus){
            return false;
        }

        ActivityDO activityDO = activityMapper.selectByActivityId(activityId);
        if(null == activityDO) {
            return false;
        }

        //当前活动状态与传入状态一致，不修改
        if(targetStatus.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }

        //需判断奖品是否全部抽完
        //查询INIT状态的奖品
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count > 0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActicityId(),
                    convertActivityStatusDTO.getTargetActivityStatus().name());
            return true;
        }catch (Exception e){
            return false;
        }

    }
}
